package view.controller;

import common.Message;
import common.NowUser;
import common.User;
import dao.AddUser;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import server.ClientConnServer;
import tools.GetId;
import utils.MD5;
import view.Login;

import javax.swing.*;

import java.io.IOException;
import java.util.concurrent.BrokenBarrierException;
import java.util.regex.Pattern;

public class Register {
    public Button registerButton;
    public Button returnButton;
    public TextField getName;
    public TextField getEmail;
    public PasswordField getPassword;
    public PasswordField getAgainPassword;
    Pattern password_pattern =Pattern.compile("[^ [A-Za-z0-9]+$]{6,16}");

    public void registerbt(){
        if(getName.getText().equals("")){
            JOptionPane.showMessageDialog(null,"用户名不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(getEmail.getText().equals("")){
            JOptionPane.showMessageDialog(null,"邮箱不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(getPassword.getText().equals("")){
            JOptionPane.showMessageDialog(null,"密码不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(!password_pattern.matcher(getPassword.getText()).matches()){
            JOptionPane.showMessageDialog(null, "密码格式错误！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(getAgainPassword.getText().equals("")){
            JOptionPane.showMessageDialog(null,"重复密码不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(!getAgainPassword.getText().equals(getPassword.getText())){
            JOptionPane.showMessageDialog(null,"两次密码不一致！","提示",JOptionPane.ERROR_MESSAGE);
        }else{
            User user = new User();
            MD5 md5 = new MD5();
            user.setPsw(md5.generateMD5(getPassword.getText()));
            user.setEmail(getEmail.getText());
            user.setName(getName.getText());
            String id = new GetId().getId();
            user.setId(id);
//            Message msg1 = new Message();
//            msg1.setUser(user);
//            msg1.setType("register");
            boolean flag = new AddUser().adduser(user);
            if (flag) {
                JOptionPane.showMessageDialog(null, "恭喜你，注册成功!账号为： " + user.getId(), "提示",JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage) registerButton.getScene().getWindow();
                stage.close();
                Login rj = new Login();
                try {
                    rj.start(stage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                JOptionPane.showMessageDialog(null, "注册失败!", "提示",JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void returnbt() {
        Stage stage = (Stage)returnButton.getScene().getWindow();
        stage.close();
        Login rj = new Login();
        try{
            rj.start(stage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
